import java.awt.Point;
import java.util.HashSet;
import java.util.Set;


public class ServerData implements PlayerListener {

	
	private Table table;
	
	private Set<Card> heldCards = new HashSet<Card>();	//cards controlled by client
	private Set<Card> selectedCards = new HashSet<Card>();	//selected cards: may or may not contain some or all of held cards
	
	//array of card id's for sending over network
	int[] selectedCardIds;
	int[] heldCardIds;
	
	public ServerData (Table table) {
		this.table = table;
	}
	

	/**
	 * Drops any selected cards.
	 * 
	 * @param The cards to drop
	 */
	public void dropSelection(Set<Card> cards) {
		for (Card card: selectedCards) {
			heldCards.remove(card);
		}
	}


	/**
	 * Flips all of the held cards.
	 */
	public void flip() {
		for (Card card: heldCards) {
			card.toggleFaceUp();
		}
	}


	/**
	 * Returns the {@link Table}
	 */
	public Table getTable() {
		return table;
	}

	
	/**
	 * Picks up the selected cards.
	 */
	public void hold() {
		for(Card card: selectedCards) {
			heldCards.add(card);
		}
		System.out.println("heldcardssize: " + heldCards.size());
		
		heldCardIds = new int[heldCards.size()];
		int i = 0;
		
		for (Card c: heldCards) {
			heldCardIds[i++] = c.getID();
		}
	}


	/**
	 * Moves the held cards by a <code>Point</code>
	 */
	public boolean moveBy(Point point) {
		boolean success = true;
		
		for(Card card: heldCards) {
			success = success && card.moveBy(point);
		}
		return success;
	}
	
	/**
	 * Moves the held cards by x, y
	 */
	public void moveBy(int x, int y) {
		for(Card card: heldCards) {
			card.moveBy(x, y);
		}
	}

	/**
	 * Deselects the selected cards.
	 */
	public void release() {
		selectedCards.clear();
		heldCards.clear();
	}

	
	/**
	 * Adds cards to the selected cards.
	 * 
	 * @param The cards to add.
	 */
	public void select(Set<Card> cards) {
		selectedCards.addAll(cards);
		
		selectedCardIds = new int[selectedCards.size()];
		int i = 0;
		
		for (Card c: selectedCards) {
			selectedCardIds[i++] = c.getID();
		}
	}


	//snapping should be implemented on the client side only??
	public void setSnap(boolean snap) {
		// TODO Auto-generated method stub

	}
	
	public int[] getHeldCardIds () {
		return selectedCardIds;
	}
	
	
	public int[] getSelectedCardIds() {
		// TODO Auto-generated method stub
		return null;
	}


	public void shuffle() {
		// TODO Auto-generated method stub
		
	}
}
